from django.views import View
from rest_framework import status
from rest_framework.permissions import AllowAny
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
from .models import DemoBook
from rest_framework.response import Response
from .serializer import DemoBookSerializer


# --------------------------------------------基本ApiView--------------------------------------------


class BookApiView(APIView):
    def get(self, request):
        data = DemoBook.objects.all()
        serializer = DemoBookSerializer(instance=data, many=True)
        return Response(serializer.data)

    def post(self, request):
        serializer = DemoBookSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.validated_data, status=status.HTTP_201_CREATED)


class BookInfoApiView(APIView):
    def get(self, request, pk):
        try:
            data = DemoBook.objects.get(pk=pk)
        except DemoBook.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = DemoBookSerializer(instance=data)
        return Response(serializer.data)

    def put(self, request, pk):
        try:
            instance_data = DemoBook.objects.get(pk=pk)
        except DemoBook.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = DemoBookSerializer(instance=instance_data, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.validated_data)

    def delete(self, request, pk):
        DemoBook.objects.get(pk=pk).delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


# --------------------------------------------GenericApiView--------------------------------------------
from rest_framework.generics import GenericAPIView


class BookGenericApiView(GenericAPIView):
    queryset = DemoBook.objects.all()
    serializer_class = DemoBookSerializer

    def get(self, request):
        data = self.get_queryset()
        serializer = DemoBookSerializer(instance=data, many=True)
        return Response(serializer.data)

    def post(self, request):
        serializer = DemoBookSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.validated_data)


class BookInfoGenericApiView(GenericAPIView):
    queryset = DemoBook.objects.all()
    serializer_class = DemoBookSerializer

    def get(self, request, pk):
        data = self.get_object()
        serializer = DemoBookSerializer(instance=data)
        return Response(serializer.data)

    def put(self, request, pk):
        data = self.get_object()
        serializer = DemoBookSerializer(instance=data, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.validated_data)

    def delete(self, request, pk):
        self.get_object().delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


# --------------------------------------------MixinsApiView--------------------------------------------
from rest_framework import mixins
from rest_framework import generics


class BookRetrieveApiView(GenericAPIView, mixins.ListModelMixin, mixins.CreateModelMixin):
    queryset = DemoBook.objects.all()
    serializer_class = DemoBookSerializer

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class BookInfoRetrieveApiView(GenericAPIView, mixins.UpdateModelMixin,
                              mixins.DestroyModelMixin, mixins.RetrieveModelMixin):
    queryset = DemoBook.objects.all()
    serializer_class = DemoBookSerializer

    def get(self, request, pk):
        return self.retrieve(request, pk)

    def put(self, request, pk):
        return self.update(request, pk)

    def delete(self, request, pk):
        return self.destroy(request, pk)


class BookGenericeApiView(generics.ListCreateAPIView):
    queryset = DemoBook.objects.all()
    serializer_class = DemoBookSerializer

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class BookInfoGenericeApiView(generics.RetrieveUpdateDestroyAPIView):
    queryset = DemoBook.objects.all()
    serializer_class = DemoBookSerializer

    def get(self, request, pk):
        return self.retrieve(request, pk)

    def put(self, request, pk):
        return self.update(request, pk)

    def delete(self, request, pk):
        return self.destroy(request, pk)


# --------------------------------------------ModelViewSetApiView--------------------------------------------
from rest_framework.viewsets import ModelViewSet


class BookModelViewSetApiView(ModelViewSet):
    queryset = DemoBook.objects.all()
    serializer_class = DemoBookSerializer
    permission_classes = [AllowAny]  # 开放所有权限
    throttle_classes = [UserRateThrottle]
